package cn.com.chengmandian.job.configurations;

import cn.com.chengmandian.core.web.utils.SpringContextUtil;
import cn.com.chengmandian.job.properties.JobProperties;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class JobConfiguration {
    @Autowired
    private JobProperties jobProperties;

    @Bean
    public XxlJobSpringExecutor xxlJobExecutor() {
        if (!jobProperties.isEnable()) {
            log.info("xxl-job not run!");
            return null;
        }
        log.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(jobProperties.getAdmin().getAddresses());
        xxlJobSpringExecutor.setAppname(SpringContextUtil.getAppName());
        xxlJobSpringExecutor.setPort(jobProperties.getExecutor().getPort());

        return xxlJobSpringExecutor;
    }
}
